//
//  InstituTypeShowView.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/10/13.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "InstituTypeShowView.h"
#import "ScreeningCell.h"

@implementation InstituTypeShowView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self createAndLayoutCollectionView];
    }
    return self;
}

-(void)createAndLayoutCollectionView{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake(scaleLeftAndRight(75), scaleTopAndBottom(30));
    self.flowLayout.minimumLineSpacing = 10;
    self.flowLayout.minimumInteritemSpacing = 10;
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.flowLayout];
    [self addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.allowsMultipleSelection = YES;
    self.collectionView.scrollEnabled = NO;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[ScreeningCell class] forCellWithReuseIdentifier:@"ScreeningCell"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
//    [self.collectionView reloadData];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    NSLog(@"这是所有的数据吗%@",self.dataArray);
    NSMutableArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:@"typeShowData"];
    return array.count;
//    return self.dataArray.count;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSMutableArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:@"typeShowData"];
    NSDictionary *dic = array[section];
    NSArray *arra = [dic allValues][0];
    NSLog(@"展示数组%@",arra);
    return arra.count ;
//    NSDictionary *dic = self.dataArray[section];
//    NSArray *arra = [dic allValues][0];
//    NSLog(@"展示数组%@",arra);
//    return arra.count ;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ScreeningCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ScreeningCell" forIndexPath:indexPath];
   NSMutableArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:@"typeShowData"];
    NSDictionary *dic = array[indexPath.section];
//    NSDictionary *dic = self.dataArray[indexPath.section];
    NSString *key = [dic allKeys][0];
    NSArray *values = dic[key];
    NSLog(@"想要的数组%@",values);
    cell.label.text = values[indexPath.row];
    cell.label.font = [UIFont systemFontOfSize:13];
    cell.label.layer.borderColor = [UIColor orangeColor].CGColor;
    cell.label.textColor = [UIColor orangeColor];
    return cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 15, 0, 15);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(scaleLeftAndRight(75), scaleTopAndBottom(30));
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        for (UIView *views in view.subviews) {
            [views removeFromSuperview];
        }
           NSMutableArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:@"typeShowData"];
         NSDictionary *dic = array[indexPath.section];
//        NSDictionary *dic = self.dataArray[indexPath.section];
        [self createTittleLabel:view WithTitle:[dic allKeys][0]];
        [self createTittleLabel:view WithTitle:[self.keysArray objectAtIndex:indexPath.section]];
        self.label.tag = 1000 + indexPath.section;
        return view;
    }else{
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        for (UIView *views in view.subviews) {
            [views removeFromSuperview];
        }
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = [UIColor lightGrayColor];
        [view addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view.mas_left).offset(15);
            make.right.equalTo(view.mas_right).offset(-15);
            make.height.mas_equalTo(0.5);
            make.centerY.equalTo(view.mas_centerY);
        }];
        return view;
    }
    
}

-(void)createTittleLabel:(UICollectionReusableView *)view WithTitle:(NSString *)title{
    self.label = [[UILabel alloc] init];
    _label.textAlignment = NSTextAlignmentCenter;
    _label.adjustsFontSizeToFitWidth = YES;
    _label.text = title;
    _label.font = [UIFont systemFontOfSize:13];
    _label.layer.borderWidth = 1.0f;
    _label.layer.borderColor = [UIColor orangeColor].CGColor;
    _label.textColor = [UIColor orangeColor];
    [view addSubview:_label];
    [_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(15);
        make.width.mas_equalTo(scaleLeftAndRight(75));
        make.top.equalTo(view.mas_top).offset(10);
        make.bottom.equalTo(view.mas_bottom).offset(-10);
    }];
    _label.layer.cornerRadius = 4;
    _label.layer.masksToBounds = YES;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(WIDTH - 30, scaleTopAndBottom(15));
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(scaleLeftAndRight(75) + 15, scaleTopAndBottom(30) + 20);
}
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

@end
